﻿#ifndef PST_FIELDSCALARBARGENERATOR_H
#define PST_FIELDSCALARBARGENERATOR_H

#include <QObject>
#include <QVector>
#include <vtkSmartPointer.h>

class vtkObject;
class vtkMapper;
class vtkRenderer;
class vtkLookupTable;
class vtkScalarBarActor;
class vtkScalarsToColors;
class vtkDataSetMapper;
class vtkCallbackCommand;
class vtkPolyDataMapper;
class vtkScalarBarWidget;

namespace pst
{
    class FieldGraphGenerator;
    class ScalarBarSettingData;

    class FieldScalarBarGenerator : public QObject
    {
        Q_OBJECT

    public:
        // parent 默认为 nullptr
        explicit FieldScalarBarGenerator(vtkRenderer* renderer, QObject* parent = nullptr);
        ~FieldScalarBarGenerator() override = default;

        // 注册一个 mapper 进来，共享同一个 LookupTable
        void addGraph(FieldGraphGenerator* graphGenerator);
        void removeAGraph(FieldGraphGenerator* graphGenerator);

        vtkSmartPointer<vtkLookupTable> getLookupTable() const { return m_lookupTable; }
        vtkSmartPointer<vtkScalarBarActor> getScalarBarActor() const { return m_scalarBar; }
        vtkSmartPointer<vtkScalarBarWidget> getScalarBarWidget() const { return m_scalarsScalarBarWidget; }

        //void setScalarBarSettingData(ScalarBarSettingData* data); 
        ScalarBarSettingData* getScalarBarSettingData() const { return m_scalarBarSettingData; }

    signals:
        // 每当全局 scalar 范围更新时发出
        void rangeChanged(double globalMin, double globalMax);

    private slots:
        // 真正的更新逻辑
        void updateRange();

    private:
        // VTK 回调桥：static -> member
        static void staticRenderCallback(vtkObject* caller,
            unsigned long eventId,
            void* clientData,
            void* callData);

        vtkSmartPointer<vtkRenderer> m_renderer;
        vtkSmartPointer<vtkLookupTable> m_lookupTable;
        vtkSmartPointer<vtkScalarBarActor> m_scalarBar;
        vtkSmartPointer<vtkCallbackCommand> m_callbackCommand;
        QVector<FieldGraphGenerator*> m_graphGenerators;
        vtkSmartPointer<vtkScalarBarWidget> m_scalarsScalarBarWidget;

        ScalarBarSettingData* m_scalarBarSettingData; // 用于存储设置数据
    };
}

#endif
